using System;
using System.Collections.Generic;
using Gj.Galaxy.Logic;
using Gj.MapData.Strategy;
using UnityEngine;

namespace Gj.MapData
{
    public class Demo
    {
        public static void BaseDataTest()
        {
            var data = new BaseData();
            data.DataType = DefineType.Unit;
            data.Id = 1;
            var b = data.Bytes();
            foreach (var bb in b) Console.WriteLine(bb);
        }

        public static void DataTypeTest()
        {
            var a = DefineType.Account;
            var b = new DataType(DefineType.AccountByte);
            var v = a.Equals(b);
            Console.Out.Write(v);
        }

        public static void Direction()
        {
            var aa = new Vector2Int(0, 0);
            var aD = new Vector2Int(0, -1);

            var bb = new Vector2Int(0, -1);
            var bD = new Vector2Int(-1, -1);

            var start1 = HexCube.FromOffset(aa).ToPosition();
            var end1 = HexCube.FromOffset(aa + aD).ToPosition();
            var a = end1 - start1;

            var start2 = HexCube.FromOffset(bb).ToPosition();
            var end2 = HexCube.FromOffset(bb + bD).ToPosition();
            var b = end2 - start2;
            Console.Out.Write(end2);
            Console.Out.Write(start2);
            Console.Out.Write(Vector3.Angle(a, new Vector3(0.8f, 0, 0.4f)));
            // > 90是正面，< 90 是背面
        }

        public static void Test()
        {
            var dataNodes = new List<DataNode>();
            dataNodes.Add(new DataNode
            {
                Position = new PositionInt(),
                Type = 1
            });
            var map = MapBuilder.Get()
                // 设置地图模式
                .Mode(MapMode.RowHex)
                // // 创建地图后的地图初始化数据
                //     .Init(OperateWrap.Get().CreateBuild(0, new PositionInt(), ExtendContent.Sync(null), InfoContent.Empty()).Builder())
                // // 设置地图计划任务，定时60秒
                //     .Schedule(ScheduleBuilder.Get()
                //         .Repeat(60, EventBuilder.Get()
                //             .Task(TaskBuilder.Get().One(0, new TaskDemo())
                //             )))
                // 设置地图数据
                .SetDataNode(dataNodes);
            // Game.single.StartCoroutine(MapBuilder.CreateMap("", map.Data(), 3, (mapId) =>
            // {
            //     
            // }));


            // 玩家信息
            var executor = Executor.Instance();

            // 申请任务：开启5个线程执行
            var taskExecutor = TaskExecutor.Dispatch(100, 10, 100, false, async e =>
            {
                switch (e.Type)
                {
                    default:
                        var content = new TaskDemo();
                        e.Parse(content);
                        break;
                }

                var account = await e.AsyncAccount(1);
                // 处理完自动结束
            });
            // 渲染需要
            var render = StrategyRender.Instance(null, 10, null);
            CooperateConnect.SetChess(render);
            CooperateConnect.BindTask(taskExecutor);
            CooperateConnect.Connect(executor, render, null, success =>
            {
                if (!success)
                {
                    taskExecutor.Stop();
                    return;
                }

                taskExecutor.Start();

                // Update时执行：
                render.Update(1);

                // 申请ai账号，可多次申请
                executor.Apply("", 0, ai =>
                {
                    // ai数量足够
                    if (ai == null) return;
                    ai.OperateBuilder()
                        .Delete(null, DefineType.Unit, 1)
                        .Operate(0, ai.CheckBuilder()
                            .And(DefineType.Unit, 1).Exist().Position(DefineType.Position, Vector2Int.down)
                            .Or().And(DefineType.Account, 1).Data(DefineType.Identity, 5)
                        );
                    // 添加一个key的计划任务
                    ai.ScheduleBuilder().Bind(DefineType.Build, 5, "key", TimeTools.FormatTime(10))
                        .Once(ai.EventBuilder().Operate(ai.OperateBuilder())).Schedule();
                    // 延迟5秒清除key的计划任务
                    ai.OperateBuilder().Update(new BaseData { DataType = DefineType.Build, Id = 2 })
                        .RemoveSchedule("key").Operate(TimeTools.FormatTime(10f));
                });

                // 添加一次计划: 30秒后执行增加资源
                // 先绑定资源事件0
                executor.EventBuilder().Operate(executor.OperateBuilder()
                    .Update(executor.Account)
                    .IncrList(DefineType.ShowExtend, 1, 100)
                    .Update(executor.Account)
                    .ChangeList(DefineType.ShowExtend, 2, 0));

                // 添加执行资源事件的计划任务
                executor.ScheduleBuilder().Bind(DefineType.Unit, 1, "resource", 30)
                    .Once(executor.EventBuilder().Event().AddEvent(0)).Schedule();

                // 添加移动计划：每10秒执行单元格移动
                // 建立建筑的攻击监听
                executor.OperateBuilder().Update(new BaseData { DataType = DefineType.Build, Id = 2 })
                    .AddRelation("attack", DefineType.Node, new Vector2Int()).Operate(0);
                // 监听绑定事件
                // executor.RelationEvent(DataType.Build, 2, "attack", 5);
                // 事件触发执行异步任务
                executor.EventBuilder().Task(executor.TaskBuilder().One(55, new TaskDemo()));

                // 对应移动单元事件
                executor.EventBuilder().Task(executor.TaskBuilder().One(55, new TaskDemo()));

                // 对应到达事件
                executor.EventBuilder().Task(executor.TaskBuilder().One(55, new TaskDemo()));
                // 每次移动会触发事件3，并触发监听attack的其他单位，到达后执行0事件
                executor.ScheduleBuilder().Bind(DefineType.Unit, 1, "move", 30)
                    .Move(null);
            });
        }
    }
}